package book.bookdao.imp;

import book.bookdao.BookDao;
import book.bookentity.Book;
import book.factory.SQLExecutorFactory;
import edu.nf.SQLExecutor;
import edu.nf.handler.BeanListHandler ;
import edu.nf.handler.BeanHandler;

import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl implements BookDao {
    /**
     * @see :通过一个Book对象添加一本图书
     */
    public int insertBook(Book book) {
        int i=0;
        String sql="insert into book(book_name,book_author,book_putaway,book_price,book_status,book_visits,book_type,book_deluser,book_deldate,book_delremark,book_hold01,book_hold02,book_hold03) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        SQLExecutor sse = SQLExecutorFactory.getSingletonSQLExecutor();
        try {
            i= sse.executeUpdate(sql,book.getId(),book.getBookName(),book.getBookAuthor(),book.getBookPutaway(),book.getBookPrice(), book.getBookStatus(),book.getBookVisits(),book.getBookType(),book.getBookDeluser(), book.getBookDeldate(),book.getBookDelremark(),book.getBookHold01(),book.getBookHold02(),book.getBookHold03());
        } catch (SQLException e) {
            throw new RuntimeException("Exception in insertBook.", e);
        }
        return i;
    }

    /**
     * @see :查看所有的图书（不包括下架的）
     */
    public List<Book> selectAllBook(){
        List<Book> list;
        String sql="select * from book where book_status != ?";
        SQLExecutor sse = SQLExecutorFactory.getPrototypeSQLExecutor();
        BeanListHandler handler = new BeanListHandler(Book.class);
        try {
            list = sse.executeQuery(sql, handler,2);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in selectAllBook.", e);
        }
        return list;
    }

    /**
     * @see :按状态，类型查看书籍
     * Parameter int status 状态
     * Parameter String str_desc  排序
     */
    public List<Book> selectTypeBook(int status ,String str_desc) {
        List<Book> list;
        String sql="select * from book where book_status=? order by "+str_desc+" desc";
        SQLExecutor sse = SQLExecutorFactory.getSingletonSQLExecutor();
        BeanListHandler handler = new BeanListHandler(Book.class);
        try {
            list = sse.executeQuery(sql, handler , status);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in selectTypeBook.", e);
        }
        return list;
    }

    /**
     * @see :按状态，类型查看书籍
     * Parameter int status 状态
     * Parameter String type 类型
     * Parameter String str_desc  排序
     */
    public List<Book> selectTypeBook(int status , int type ,String str_desc) {
        List<Book> list;
        String sql="select * from book where book_status=? and book_type=? order by "+str_desc+" desc";
        SQLExecutor sse = SQLExecutorFactory.getSingletonSQLExecutor();
        BeanListHandler handler = new BeanListHandler(Book.class);
        try {
            list = sse.executeQuery(sql, handler , status ,type);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in selectTypeBook.", e);
        }
        return list;
    }

    /**
     * @see :按状态模糊搜索 书名
     * Parameter int status 状态
     * Parameter String name 书名
     * Parameter String str_desc  排序
     */
    public List<Book> selectStrnameBook(int status , String name,String str_desc) {
        List<Book> list;
        String sql="select * from book where book_status=? and book_name like ? order by "+str_desc+" desc";
        SQLExecutor sse = SQLExecutorFactory.getSingletonSQLExecutor();
        BeanListHandler handler = new BeanListHandler(Book.class);
        try {
            list = sse.executeQuery(sql, handler , status,name,str_desc);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in selectStrnameBook.", e);
        }
        return list;
    }

    /**
     * @see :通过id搜索book
     * Parameter id
     */
    public Book selectBook(int id){
        Book book;
        String sql="select * from book where id=?";
        SQLExecutor sse=SQLExecutorFactory.getSingletonSQLExecutor();
        BeanHandler handler=new BeanHandler(Book.class);
        try {
            book = sse.executeQuery(sql ,handler , id);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in selectBook.", e);
        }
        return book;
    }
    /**
     * @see :修改书的信息
     * Parameter Book book
     */
    public int updateBook(Book book) {
        int i=0;
        String sql="update book set book_name=?,book_author=?,book_putaway=?,book_price=?,book_status=?,book_visits=?,book_type=?,book_deluser=?,book_deldate=?,book_delremark=?,book_hold01=?,book_hold02=?,book_hold03=? where id=?";
        SQLExecutor sse = SQLExecutorFactory.getSingletonSQLExecutor();
        try {
            i=sse.executeUpdate(sql,book.getBookName(),book.getBookAuthor(),book.getBookPutaway(),book.getBookPrice(), book.getBookStatus(),book.getBookVisits(),book.getBookType(),book.getBookDeluser(), book.getBookDeldate(),book.getBookDelremark(),book.getBookHold01(),book.getBookHold02(),book.getBookHold03(),book.getId());
        } catch (SQLException e) {
            throw new RuntimeException("Exception in updateBook.", e);
        }
        return i;
    }
}
